package com.test.hand.server.handler.quit;

import com.test.hand.server.session.SessionFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@ChannelHandler.Sharable
public class QuitHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        try {
            // 解除session的绑定
            SessionFactory.getSession().unbind(ctx.channel());
        } catch (NullPointerException e) {
            log.debug("此次退出的用户，未进行登录");
        }
        log.info("用户断开-channelInactive success");

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        // 解除session的绑定
        SessionFactory.getSession().unbind(ctx.channel());
        log.info("用户异常断开-channelInactive success");

    }
}
